// world.querySelectorAll('player').forEach((e) => {if(e.player.name !=='坦率的血翼蝠5801')return;e.addTag('有电')})
import i18n from '@root/i18n'

const ks = world.querySelector('#开锁器')
if (ks) {
    ks.enableInteract = true
    ks.interactRadius = 3
    ks.interactHint = `开锁器 Lockpick`
    ks.onInteract(async ({ entity }) => {
        if (entity.bq.includes(i18n.t("lockpick_message", { lng: entity.player.language }))) return
        entity.player.directMessage(i18n.t("lockpick_message", { lng: entity.player.language }))
        entity.addTag('ks')
        entity.bq.push(i18n.t("lockpick_message", { lng: entity.player.language }))
    })
}

world.onPlayerJoin(async ({ entity }) => {
    entity.bq = []
})

const lz = world.querySelector('#乌鸦')
if (lz) {
    lz.enableInteract = true
    lz.interactRadius = 5
    lz.interactHint = `乌鸦 Crow`

    lz.onInteract(async ({ entity }) => {
        if (entity.hasTag('火')) {
            entity.player.directMessage(i18n.t("crow_message_1", { lng: entity.player.language }))
            entity.bq.push('进入下水道')
            entity.addTag('下水道')
        } else {
            entity.player.directMessage(i18n.t("crow_message_2", { lng: entity.player.language }))
        }
    })
}



const tree = world.querySelector('#木板-1')
if (tree) {
    tree.enableInteract = true
    tree.interactRadius = 5
    tree.interactHint = `木板 Wooden Plank`

    tree.onInteract(async ({ entity }) => {
        if (entity.onHand == (entity.player.language === 'en' ? 'Glass Fuse' : '玻璃保险丝')) {
            entity.removeTag('notree')
            entity.bq.push('走三楼木板')
            entity.player.directMessage(i18n.t("wood_message_1", { lng: entity.player.language }))
        } else {
            entity.player.directMessage(i18n.t("wood_message_2", { lng: entity.player.language }))
        }
    })
}

const button = world.querySelector('#按钮')
if (button) {
    button.enableInteract = true
    button.interactRadius = 5
    button.interactHint = `按钮 Button`

    button.onInteract(async ({ entity }) => {
        if (entity.hasTag('有电')) {
            entity.bq.push('底盖开启')
            entity.addTag('底盖')
            entity.player.directMessage(i18n.t("button_message_1", { lng: entity.player.language }))
        } else {
            entity.player.directMessage(i18n.t("button_message_2", { lng: entity.player.language }))
        }
    })
}
const fire = world.querySelector('#火')
if (fire) {
    fire.enableInteract = true
    fire.interactRadius = 5
    fire.interactHint = `壁炉 Fireplace`

    fire.onInteract(async ({ entity }) => {
        // 生火要改
        if (entity.onHand !== `火柴`) {
            entity.player.directMessage(i18n.t("fire_message_1", { lng: entity.player.language }))
            return;
        }
        entity.addTag('火')
        entity.bq.push('生火成功！')
        // 直接根据玩家语言设置空手状态文本
        entity.onHand = entity.player.language === 'en' ? 'Nothing' : '空气'
        entity.player.directMessage(i18n.t("fire_message_2", { lng: entity.player.language }))
    })
}

const he = world.querySelector('#火柴盒')
if (he) {
    he.enableInteract = true
    he.interactRadius = 3
    he.interactHint = `火柴盒 Matchbox`

    he.onInteract(async ({ entity }) => {
        // 生火要改
        // 根据玩家语言显示正确的放回物品消息
        const putBackMessage = entity.player.language === 'en'
            ? `You took out a match, put ${entity.onHand === 'Nothing' ? 'Nothing' : entity.onHand} back`
            : `已获得火柴×1，${entity.onHand === '空气' ? '空气' : entity.onHand}放回原位`;

        entity.player.directMessage(putBackMessage);
        entity.onHand = entity.player.language === 'en' ? 'Match' : '火柴'
    })
}

const dead = world.querySelector('#尸体')
if (dead) {
    dead.enableInteract = true
    dead.interactRadius = 4
    dead.interactHint = `检查 Check`
    dead.onInteract(async ({ entity }) => {
        entity.player.directMessage(i18n.t("dead_message", { lng: entity.player.language }))
    })
}



const dl = world.querySelector('#主电闸')
if (dl) {
    dl.enableInteract = true
    dl.interactRadius = 3
    dl.interactHint = `巨大的电力闸 Giant Power Switch`

    dl.onInteract(async ({ entity }) => {
        if (entity.key >= 4) {
            entity.addTag('有电')
            entity.bq.push('有电')
            entity.player.directMessage(i18n.t("power_message_1", { lng: entity.player.language }))
        } else {
            entity.player.directMessage(i18n.t("power_message_2", { lng: entity.player.language, count: entity.key }))
        }
    })
}

const gs = world.querySelector('#钢丝')
if (gs) {
    gs.enableInteract = true
    gs.interactRadius = 5
    gs.interactHint = `玻璃保险丝 Glass Fuse`
    gs.onInteract(async ({ entity }) => {
        entity.player.directMessage(i18n.t("fuse_message", { lng: entity.player.language, item: entity.onHand }))
        entity.onHand = entity.player.language === 'en' ? 'Glass Fuse' : '玻璃保险丝'
    })
}

const dianti = world.querySelector('#电梯')

world.querySelector('.door')?.onEntityContact(async ({ other, entity }) => {
    if (!(other.player)) return;
    if (other.hasTag('go')) {
        const playerName = other.player.name;
        world.querySelectorAll('player').forEach((e) => { e.player.directMessage(i18n.t("escape_message_1", { lng: e.player.language, playerName })) })
        other.hp = other.maxHp
        other.player.emissive = 1
        other.player.canFly = true
        other.player.spectator = true
        other.removeTag('吸引')
        other.bq.push('不被敌人看见')
        other.position.copy(other.player.spawnPoint)
        other.wq = 0
    } else {
        other.player.directMessage(i18n.t("door_message_1", { lng: other.player.language }))
    }
})

world.querySelector('#木屋门')?.onEntityContact(async ({ other, entity }) => {
    if (!(other.player)) return;
    other.player.directMessage(i18n.t("door_message_2", { lng: other.player.language }))
})

world.querySelector('#下水道门')?.onEntityContact(async ({ other, entity }) => {
    if (!(other.player)) return;
    other.player.directMessage(i18n.t("door_message_3", { lng: other.player.language }))
})


world.querySelector('#底盖')?.onEntityContact(async ({ other, entity }) => {
    if (!(other.player)) return;
    if (other.hasTag('底盖')) {
        other.player.directMessage(i18n.t("cover_message_1", { lng: other.player.language }))
        await sleep(1000)
        const playerName = other.player.name;
        world.querySelectorAll('player').forEach((e) => { e.player.directMessage(i18n.t("escape_message_1", { lng: e.player.language, playerName })) })
        other.hp = other.maxHp
        other.player.emissive = 1
        other.player.canFly = true
        other.player.spectator = true
        other.removeTag('吸引')
        other.bq.push('不被敌人看见')
        other.wq = 0
    } else {
        other.player.directMessage(i18n.t("cover_message_2", { lng: other.player.language }))
    }
})
// const yz = world.querySelector('#椰子')
// if (yz) {
//     yz.enableInteract = true
//     yz.interactRadius = 5
//     yz.interactHint = entity.player.language === 'en' ? `Coconut` : `椰子`
//     yz.onInteract(async({entity}) => {
//         const message = entity.player.language === 'en' 
//             ? `You got a coconut, put ${entity.onHand} back` 
//             : `已获得椰子×1，${entity.onHand}放回原位`;
//         entity.player.directMessage(message)
//         entity.onHand = entity.player.language === 'en' ? 'Coconut' : '椰子'
//     })
// }

const dt = world.querySelector('#断头台')
if (dt) {
    dt.enableInteract = true
    dt.interactRadius = 5
    dt.interactHint = `断头台 Guillotine`
    dt.onInteract(async ({ entity }) => {
        if (entity.onHand == (entity.player.language === 'en' ? 'Coconut' : '椰子')) {
            entity.player.directMessage(i18n.t("guillotine_message_1", { lng: entity.player.language, item: entity.onHand }))
            entity.onHand = entity.player.language === 'en' ? 'Teddy Bear' : '泰迪熊'
        } else {
            entity.player.directMessage(i18n.t("guillotine_message_2", { lng: entity.player.language }))
        }
    })
}
// const yl = world.querySelector('#摇篮')
// if (yl) {
//     yl.enableInteract = true
//     yl.interactRadius = 5
//     yl.interactHint = entity.player.language === 'en' ? `Slendrina's Cradle` : `兰德里纳河的摇篮`
//     yl.onInteract(async({entity}) => {
//         if(entity.onHand == (entity.player.language === 'en' ? 'Teddy Bear' : '泰迪熊')){
//             const message = entity.player.language === 'en' 
//                 ? `You put the teddy bear in the cradle, Slendrina seems to have appeared... He won't hurt you anymore...` 
//                 : `你把泰迪熊放入了摇篮，兰德里纳河似乎出现了……他不会再伤害你了……`;
//             entity.player.directMessage(message)
//             entity.onHand = entity.player.language === 'en' ? 'Nothing' : '空气'
//             entity.addTag('摇篮')
//             entity.bq.push(entity.player.language === 'en' ? 'Slendrina won't attack' : '兰德里纳河不再打')
//         }else{
//             const message = entity.player.language === 'en' 
//                 ? `This cradle is warm and scary... What needs to be placed in it...` 
//                 : `这个摇篮温馨而恐怖……需要什么东西放在其中……`;
//             entity.player.directMessage(message)
//         }
//     })
// }

// const coin = world.querySelector('#小硬币')
// if (coin) {
//     coin.enableInteract = 1
//     coin.interactRadius = 2
//     coin.interactHint = entity.player.language === 'en' ? `Safe` : `保险箱`
//     coin.onInteract(async({entity}) => {
//         if(entity.hasTag('安全钥匙')){
//             const message = entity.player.language === 'en' 
//                 ? `You got a small coin, put ${entity.onHand} back` 
//                 : `已获得小硬币×1，${entity.onHand}放回原位`;
//             entity.player.directMessage(message)
//             entity.onHand = entity.player.language === 'en' ? 'Small Coin' : `小硬币`
//         }else{
//             const message = entity.player.language === 'en' 
//                 ? `You still need a safe key` 
//                 : `你还需要安全钥匙`;
//             entity.player.directMessage(message)
//         }
//     })
// }

const computer = world.querySelector('#电脑')
if (computer) {
    computer.enableInteract = true
    computer.interactRadius = 2
    computer.interactHint = `生锈的检票机器 Rusty Ticket Machine`
    computer.onInteract(async ({ entity }) => {
        if (entity.onHand == (entity.player.language === 'en' ? 'Small Coin' : `小硬币`)) {
            entity.addTag('地铁')
            entity.bq.push('投币成功')
            // 直接根据玩家语言设置空手状态文本
            entity.onHand = entity.player.language === 'en' ? 'Nothing' : '空气'
            entity.player.directMessage(i18n.t("computer_message_1", { lng: entity.player.language }))
        } else {
            entity.player.directMessage(i18n.t("computer_message_2", { lng: entity.player.language }))
        }
    })
}

const you = world.querySelector('#油箱')
if (you) {
    you.enableInteract = true
    you.interactRadius = 2
    you.interactHint = `废弃许久的油箱 Long Abandoned Fuel Tank`
    you.onInteract(async ({ entity }) => {
        if (entity.onHand == (entity.player.language === 'en' ? 'Gasoline' : `汽油`)) {
            if (entity.hasTag('地铁')) {
                // 直接根据玩家语言设置空手状态文本
                entity.onHand = entity.player.language === 'en' ? 'Nothing' : '空气'
                entity.player.directMessage(i18n.t("tank_message_1", { lng: entity.player.language }))
                await sleep(1000)
                entity.player.directMessage(i18n.t("tank_message_2", { lng: entity.player.language }))
                await sleep(1000)
                entity.player.directMessage(i18n.t("tank_message_3", { lng: entity.player.language }))
                await sleep(1000)
                entity.player.directMessage(i18n.t("tank_message_4", { lng: entity.player.language }))
                await sleep(1000)
                entity.player.directMessage(i18n.t("tank_message_5", { lng: entity.player.language }))
                await sleep(1000)
                entity.player.directMessage(i18n.t("tank_message_6", { lng: entity.player.language }))
                await sleep(1000)
                entity.player.directMessage(i18n.t("tank_message_7", { lng: entity.player.language }))
                await sleep(640)
                entity.velocity.z -= 25
                await sleep(800)
                entity.position.z -= 6
            }
        } else {
            entity.player.directMessage(i18n.t("tank_message_8", { lng: entity.player.language }))
        }
    })
}



const you1 = world.querySelector('#加油')
if (you1) {
    you1.enableInteract = true
    you1.interactRadius = 2
    you1.interactHint = `加油 Gasoline`
    you1.onInteract(async ({ entity }) => {
        if (entity.onHand == (entity.player.language === 'en' ? 'Gasoline' : `汽油`)) return;
        entity.player.directMessage(i18n.t("gas_message", { lng: entity.player.language, item: entity.onHand }))
        entity.onHand = entity.player.language === 'en' ? 'Gasoline' : `汽油`

    })
}
world.querySelector('.subway')?.onEntityContact(async ({ other, entity }) => {
    if (!other.player) return;
    const playerName = other.player.name;
    world.querySelectorAll('player').forEach((e) => { e.player.directMessage(i18n.t("escape_message_2", { lng: e.player.language, playerName })) })
    other.player.emissive = 1
    other.player.canFly = true
    other.player.spectator = true
    other.hp = other.maxHp
    other.removeTag('吸引')
    other.bq.push('不被敌人看见')
    other.wq = 0
})

world.querySelector('.subway1')?.onEntityContact(async ({ other, entity }) => {
    if (!other.player) return;
    const playerName = other.player.name;
    world.querySelectorAll('player').forEach((e) => { e.player.directMessage(i18n.t("escape_message_2", { lng: e.player.language, playerName })) })
    other.player.emissive = 1
    other.player.canFly = true
    other.player.spectator = true
    other.hp = other.maxHp
    other.removeTag('吸引')
    other.bq.push('不被敌人看见')
    other.wq = 0
})

world.querySelector('.subway2')?.onEntityContact(async ({ other, entity }) => {
    if (!other.player) return;
    const playerName = other.player.name;
    world.querySelectorAll('player').forEach((e) => { e.player.directMessage(i18n.t("escape_message_2", { lng: e.player.language, playerName })) })
    other.player.emissive = 1
    other.hp = other.maxHp
    other.player.canFly = true
    other.player.spectator = true
    other.removeTag('吸引')
    other.bq.push('不被敌人看见')
    other.wq = 0
})


const share = world.querySelector('#海报')
if (share) {
    share.enableInteract = true
    share.interactRadius = 4
    share.interactHint = `分享 Share`

    share.onInteract(async ({ entity }) => {
        entity.player.navigator.emitEvent('share', {
            title: i18n.t("share_title", { lng: entity.player.language }),
            description: 'description',
            text: `https://dao3.fun/exp/experience/detail/100181304
        ${i18n.t("share_text", { lng: entity.player.language })}`,
        })
    })
}